﻿using System;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace smartphone.App_Code
{
    public static class LinkClass
    {
        // Builds an absolute URL
        private static string BuildAbsolute(string relativeUri)
        {
            // get current uri
            Uri uri = HttpContext.Current.Request.Url;
            // build absolute path
            string app = HttpContext.Current.Request.ApplicationPath;
            if (!app.EndsWith("/")) app += "/";
            relativeUri = relativeUri.TrimStart('/');
            // return the absolute path
            return HttpUtility.UrlPathEncode(
              String.Format("http://{0}:{1}{2}{3}",
              uri.Host, uri.Port, app, relativeUri));
        }

        // tao 1 URL hang san xuat
        public static string ToManufacturer(string manufacturerId, string page)
        {
            if (page == "1")
                return BuildAbsolute(String.Format("Manufacturer_page.aspx?ManufacturerId={0}", manufacturerId));
            else
                return BuildAbsolute(String.Format("Manufacturer_page.aspx?ManufacturerId={0}&Page={1}", manufacturerId, page));
        }

        // Generate a department URL for the first page
        public static string ToManufacturer(string manufacturerId)
        {
            return ToManufacturer(manufacturerId, "1");
        }

        //Build link den chi tiet san pham
        public static string ToProductDetail(string productId)
        {
            return BuildAbsolute(String.Format("Product_page.aspx?ProductId={0}", productId));
        }

        public static string ToProductImage(string productImg)
        {
            return BuildAbsolute("ProductImages/" + productImg);
        }

        public static string ToHomeSlideShow(string homeImg)
        {
            return BuildAbsolute("ProductImages/" + homeImg);
        }

        /// <summary>
        /// Link toi tin tuc
        /// </summary>
        /// <param name="newsId"></param>
        /// <returns></returns>
        public static string ToNews(string newsId)
        {
            return BuildAbsolute(String.Format("News_page.aspx?NewsId={0}", newsId));
        }

        /// <summary>
        /// Link de trang tim kiem
        /// </summary>
        /// <param name="searchString"></param>
        /// <param name="allWords"></param>
        /// <param name="page"></param>
        /// <returns></returns>
        public static string ToSearch(string searchString, bool allWords, string page)
        {
            if (page == "1")
            {
                return BuildAbsolute(string.Format("/Search.aspx?Search={0}&AllWords={1}", searchString, allWords));
            }
            else
            {
                return BuildAbsolute(string.Format("/Search.aspx?Search={0}&AllWords={1}&Page={2}",
                    searchString, allWords, page));
            }
        }

        public static string ToPayPalCheckout(string orderName, decimal  orderAmount)
        {
            return HttpUtility.UrlPathEncode(String.Format("{0}/business={1}&item_name={2}&amount={3:0.00}&currency ={4}&return={5}&cancel_return={6}",
                ESmartConfig.PaypalUrl,
                ESmartConfig.PaypalEmail,
                orderName,
                orderAmount,
                ESmartConfig.PaypalCurrency,
                ESmartConfig.PaypalReturnUrl,
                ESmartConfig.PaypalCancelUrl));
        }
        public static string ToPayPalAddItem(string productUrl, string productName, decimal productPrice, string productOptions)
        {
            return HttpUtility.UrlPathEncode(
            String.Format("{0}&business={1}&return={2}&cancel_return={3}&shopping_url={4}&item_name={5}&amount={6:0.00}&currency={7}&on0=Options&os0={8}&add=1",
            ESmartConfig.PaypalUrl,
            ESmartConfig.PaypalEmail,
            ESmartConfig.PaypalReturnUrl,
            ESmartConfig.PaypalCancelUrl,
            productUrl,
            productName,
                    productPrice,
            ESmartConfig.PaypalCurrency,
            productOptions));
        } 
    }
}